package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.dto.MemberPageDTO;
import cn.kgc.ssm.entity.Member;
import cn.kgc.ssm.mapper.MemberMapper;
import cn.kgc.ssm.response.ResponseResult;
import cn.kgc.ssm.service.MemberService;
import cn.kgc.ssm.utils.IDCardUtil;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/11/18
 * @description
 */
@Service

public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public ResponseResult memberPage(MemberPageDTO memberPageDTO) {

        PageHelper.startPage(memberPageDTO.getPage(), memberPageDTO.getLimit());

        List<Member> tableData = memberMapper.selectMember(memberPageDTO.getMemberName(), memberPageDTO.getIdCard());

        PageInfo<Member> memberPageInfo = new PageInfo<>(tableData);

        long total = memberPageInfo.getTotal();


        return ResponseResult.success().count(total).data(tableData);
    }

    @Override
    public ResponseResult addMember(Member member) {

        // 1.判定会员名是否存在
        String memberName = member.getMemberName();
        Member member1 = memberMapper.selectMemberByMemberName(memberName);
        if (!ObjectUtils.isEmpty(member1)) {
            return ResponseResult.fail().msg("会员名已被占用");
        }

        // 2. 设置默认密码
        String defaultPassword = "111111";
        member.setPassword(SecureUtil.md5(defaultPassword));

        // 3. 生成会员卡号    KGC20241118324567
        Member member2 = null;
        String idCard = null;
        do {
            idCard = IDCardUtil.createIDCard();
            member2 = memberMapper.selectMemberByIdCard(idCard);
        } while (!ObjectUtils.isEmpty(member2));

        member.setIdCard(idCard);
        // 4. 添加会员数据  sys_member
        memberMapper.addMember(member);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult editMember(Member member) {

        memberMapper.updateMember(member);

        return ResponseResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteMember(List<Integer> ids) {

        memberMapper.deleteMember(ids);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult registerMember(Member member) {

        //  1. 判定会员名是否存在
        Member member1 = memberMapper.selectMemberByMemberName(member.getMemberName());
        if (!ObjectUtils.isEmpty(member1)) {
            return ResponseResult.fail().msg("会员名已存在");
        }

        // 2. 密码加密
        String md5Pwd = SecureUtil.md5(member.getPassword());
        member.setPassword(md5Pwd);

        // 3. 生成会员卡号
        Member member2 = null;
        String idCard = null;
        do {
            idCard = IDCardUtil.createIDCard();
            member2 = memberMapper.selectMemberByIdCard(idCard);
        } while (!ObjectUtils.isEmpty(member2));

        member.setIdCard(idCard);

        memberMapper.registerMember(member);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult login(Member member, HttpSession session) {

        String memberName = member.getMemberName();
        Member member1 = memberMapper.selectMemberByMemberName(memberName);
        if (ObjectUtils.isEmpty(member1)) {  // true
            return ResponseResult.fail().msg("会员不存在");
        }


        String formPwd = SecureUtil.md5(member.getPassword());
        String dBPassword = member1.getPassword();

        if (!StringUtils.equals(formPwd, dBPassword)) {
            return ResponseResult.fail().msg("密码错误");
        }

        // 会员登录成功
        session.setAttribute("tel", member1.getTel());
        session.setAttribute("memberName", member1.getMemberName());
        session.setAttribute("authentication", "true");
        return ResponseResult.success().msg("认证通过");
    }
}
